home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ham Radio 2000
/
Ham Radio 2000.iso
/
ham2000
/
tcp_ip
/
gp
/
7p163_ip.exe
/
7PLSRC.LZH
/
UTILS.C
< prev
Wrap
C/C++ Source or Header
|
1991-12-22
|
11KB
|
607 lines
#include "7plus.h"
#include "globals.h"
/*
*** get a line from file. don't care about type of line separator.
***
***
***
*/
char *my_fgets (char *string, register n, FILE *rein)
{
register in, i;
while ((in = fgetc (rein)) != EOF)
{
if (in == 0x0d || in == 0x0a)
continue;
else
break;
}
if (feof (rein))
return (NULL);
string[0] = (char) in;
i = 1;
while ((in = fgetc (rein)) != EOF)
{
if (in == 0x0d || in == 0x0a)
in = 0x0a;
string[i++] = (char) in;
if (i == n || in == 0x0a)
break;
}
string[i] = EOS;
return (string);
}
/*
*** Get crc and line number from code line.
***
***
*/
void crc_n_lnum (uint *crc, int *linenumber, char *line)
{
register ulong cs;
cs = 0xb640L * decode[(byte)line[66]] +
0xd8L * decode[(byte)line[65]] +
decode[(byte)line[64]];
*linenumber = (int) (cs >> 14); /* upper 9 bits are the line number */
*crc = (uint) (cs & 0x3fffL); /* lower 14 bits are the CRC */
}
/*
*** Get crc2 from code line.
***
***
*/
void crc2 (uint *crc, char *line)
{
*crc = 0xd8 * decode[(byte)line[68]] +
decode[(byte)line[67]];
}
/*
*** Whip up 2nd CRC
***
***
*/
void add_crc2 (char *line)
{
register uint crc;
register int i;
/* Whip up 2nd CRC */
crc = 0;
for (i=66;i>-1;i--)
crc = crctab[crc>>8] ^ (((crc&255)<<8) | (byte) line[i]);
crc &= 0x7fff;
i = 67;
line[i++] = code[crc % 0xd8];
line[i++] = code[crc / 0xd8];
line[i] = EOS;
}
/*
*** mini-crc for header. safe enough...
***
***
*/
int mcrc (char *line, int flag)
{
register int i, j;
register uint crc;
char test[3], *p;
sprintf (test, "\xb0\xb1");
if ((p = strstr (line, test)) == NULL)
return (0);
j = (int) (p - line) + 4;
for (i=crc=0; i<j; i++)
crc = crctab[crc>>8] ^ (((crc&255)<<8) | (byte)line[i]);
crc %= 216;
if (!flag)
{
if (crc == (uint) decode[(byte)line[j]])
return (1);
else
return (0);
}
else
line[j] = code[(byte)crc];
return (crc);
}
/*
*** read a file, search for s1, calculate CRC until s2 is found.
*** flag == 1: compare calculated an read CRC.
*** flag == 0: append CRC to file.
*/
int crc_file (char *file, char *s1, char *s2, int flag)
{
char line[81];
uint crc, cs;
int i, j, k;
FILE *in;
crc = cs = 0;
if ((in = fopen (file, OPEN_READ_TEXT)) == NULL)
{
printf (cant, file);
exit (2);
}
i = (int) strlen (s1);
k = (int) strlen (s2);
j = 1;
do
{
if (my_fgets (line, 80, in) == NULL)
break;
j = strncmp (line, s1, i);
}
while (j);
if (j)
{
printf ("\nStart '%s' not found.\n", s1);
exit (0);
}
j = 0;
do
{
for (i=0;i!=(int)strlen(line);i++)
crc = crctab[crc>>8] ^ (((crc&255)<<8) | (byte)line[i]);
j = strncmp (line, s2, k);
if (!j)
continue;
if (my_fgets (line, 80, in) == NULL)
break;
}
while (j);
if (j)
{
printf ("\nEnd '%s' not found.\n", s2);
exit (0);
}
my_fgets (line, 80, in);
fclose (in);
/* evaluate CRC */
if (flag)
{
if (!line || strncmp ("CRC ", line, 4))
{
printf ("\n'%s': no CRC found.\n(File may be corrupted or from version \
earlier than 7PLUS v1.5)\n", file);
return (2);
}
cs = get_hex (line+4);
if (cs == crc)
return (0);
else
{
printf ("\007'%s' is corrupted. Break.\n", file);
return (1);
}
}
else
{ /* append CRC to file */
in = fopen (file, OPEN_APPEND_TEXT);
fprintf (in, "CRC %04X%s", crc, delimit);
fclose (in);
}
return (0);
}
/*
***
*** Write a byte to file.
***
*/
int my_putc (int outchar, FILE *out)
{
register x;
if ((x = putc ((char) outchar, out)) == EOF)
{
printf ("\007\nWrite error! Can't continue.\n");
exit (1);
}
return (x);
}
/*
***
***
***
*/
void kill_dest (FILE *in, FILE *out, char *name)
{
if (out)
fclose (out);
if (in)
fclose (in);
if (*name)
unlink (name);
}
/*
***
*** test if a file exists at all
***
*/
int test_exist (char *filename)
{
FILE *rein = NULL;
if ((rein = fopen (filename, OPEN_READ_TEXT)) != NULL)
{
fclose (rein);
return (0);
}
return (1);
}
/*
*** test if outputfile already exists. prompt for overwrite or
*** new name.
***
***
*/
void test_file (FILE *rein, char *destnam, int flag, int namsize)
{
FILE *raus;
char compare[30], no[] = NO, yes[] = YES;
int i;
if (noquery)
return;
sprintf (compare, "%%%d[^\n]", namsize);
/* Loop as long as file can be opened. */
while ((raus = fopen (destnam, OPEN_READ_BINARY)) != NULL)
{
printf ("\007\nOutputfile '%s' already exists, overwrite? [y/n] ", destnam);
do
{
i = getch ();
if (i == 'n' || i == 'N')
{
if (flag)
{
printf ("%s\n\nEnter new name (max %d chars)\n", no, namsize);
printf ("or simply press ENTER to break : ");
fflush (stdin);
if (namsize == 12)
strlwr (destnam);
scanf (compare, destnam);
fflush (stdin);
}
else
*destnam = EOS;
if (!strlen (destnam))
{
if (!flag)
printf ("%s\n", no);
printf ("Break.\n");
fclose (rein);
exit (10);
}
i = 0xff; /* indicate, that new name has been specified */
}
}
while (i != 'y' && i != 'Y' && i != 0xff);
if (i != 0xff)
printf ("%s\n", yes);
printf ("\n");
fclose (raus);
if (i != 0xff)
break;
}
}
/*
*** initialize decoding table
***
***
***
*/
void init_decodetab (void)
{
register i;
register byte j;
for (i = 0; i < 256; i++)
decode[i] = 255;
j = 0;
for (i = 0x21; i < 0x2a; i++)
decode[i] = j++;
for (i = 0x2b; i < 0x7f; i++)
decode[i] = j++;
for (i = 0x80; i < 0x91; i++)
decode[i] = j++;
decode[0x92] = j++;
for (i = 0x94; i < 0xfd; i++)
decode[i] = j++;
}
/*
*** initialize encoding table
***
***
***
*/
void init_codetab (void)
{
register byte i, j;
j = 0;
for (i = 0x21; i < 0x2a; i++, j++)
code[j] = i;
for (i = 0x2b; i < 0x7f; i++, j++)
code[j] = i;
for (i = 0x80; i < 0x91; i++, j++)
code[j] = i;
code[j++] = 146;
for (i = 0x94; i < 0xfd; i++, j++)
code[j] = i;
}
/*
*** Tnx to DC4OX.
***
*** calculate CRC-table
***
*/
void init_crctab (void)
{
uint m, n, r, mask;
static uint bitrmdrs[] = { 0x9188,0x48C4,0x2462,0x1231,
0x8108,0x4084,0x2042,0x1021 };
for (n = 0; n < 256; ++n)
{
for (mask = 0x0080, r = 0, m = 0; m < 8; ++m, mask >>= 1)
if (n & mask)
r = bitrmdrs[m] ^ r;
crctab[n] = r;
}
}
/*
*** Create a MSDOS/ATARI compatible filename.
***
***
***
*/
void build_DOS_name (char *name)
{
char tmp[MAXFNAME];
register i, j;
i = j = 0;
strcpy (tmp, name);
strlwr (tmp);
if (*tmp)
{
do
{
tmp[i] &= 127;
if (strchr (" <>=,;:*?&[]()/.\\\"~+@", tmp[i]) == NULL)
name[j++] = tmp[i];
}
while (tmp[++i]);
name[j] = EOS;
}
}
/*
*** get_hex: some compilers have real big trouble when reading hex values from
*** a file with fscanf() that have leading zeros! e.g. 00A will be
*** read as two separate values (0 and A)! grr!!
*** get_hex skips all leading zeros to eliminate the problem.
***
*/
uint get_hex (char *hex)
{
register i = 0;
uint ret = 0;
while (hex[i] == '0')
i++;
sscanf(hex+i, "%x", &ret);
return (ret);
}
#ifdef _FNSPLIT
/*
*** filenamesplit
*** (by DL1MEN, taken from SP-ST, modified for portability)
***
*** split filename up into drive, path, name and extension.
***
*/
void fnsplit(char *pth, char *dr, char *pa, char *fn, char *ft)
{
char drv[MAXDRIVE], pat[MAXDIR], fna[MAXFILE], fty[MAXEXT], tmp[MAXPATH];
char *p;
strcpy(tmp,pth);
if ((p = strchr(tmp,':')) != NULL)
{
*p++ = EOS;
strcpy(drv,tmp);
}
else
{
p = tmp;
drv[0] = EOS;
}
if ((pth = strrchr(p, PATHCHAR)) != NULL)
{
*pth++ = EOS;
strcpy(pat,p);
}
else
{
pth = p;
pat[0] = EOS;
}
if ((p = strchr(pth,'.')) != NULL)
{
strcpy(fty,p);
fty[MAXEXT-1] = EOS;
*p = EOS;
}
else
fty[0] = EOS;
strcpy(fna,pth);
fna[MAXFILE-1] = EOS;
if (dr)
{
strcpy(dr,drv);
if (drv[0])
strcat(dr,":");
}
if (pa)
{
strcpy(pa,pat);
if (pat[0])
strcat(pa, PATHSEP);
}
if (fn)
strcpy(fn,fna);
if (ft)
strcpy(ft,fty);
}
#endif /** _FNSPLIT **/
#ifdef _ICMP
/* The following functions are unfortunately not avialable on Aztec 5.0b,
because it sticks closely to the ANSI_C standard. You may also have
to include these functions, if compiling on other ANSI_C compilers.*/
/*
*** strupr - convert string to upper case.
***
***
*/
char *strupr (char *string)
{
char *strcnvt (char *string, int flag);
return (strcnvt (string, 1));
}
/*
*** strlwr - convert string to lower case.
***
***
*/
char *strlwr (char *string)
{
char *strcnvt (char *string, int flag);
return (strcnvt (string, 0));
}
/*
*** strcnvt - convert string to upper (flag == 1) or lower (flag == 0) case.
***
***
*/
char *strcnvt (char *string, int flag)
{
register i = 0;
while (string[i])
{
string[i] = (flag)?toupper (string[i]):tolower (string[i]);
i++;
}
return (string);
}
/*
*** stricmp - same as strcmp(), but ignores case.
*** s1 and s2 are not modified.
***
*/
int stricmp (char *s1, char *s2)
{
return (strnicmp (s1, s2, (size_t) 80));
}
/*
*** strnicmp - same as strncmp(), but ignores case.
*** s1 and s2 are not modified.
***
*/
int strnicmp (char *s1, char *s2, int n)
{
char _s1[81], _s2[81];
strncpy (_s1, s1, 80);
strncpy (_s2, s2, 80);
strupr (_s1);
strupr (_s2);
return (strncmp (_s1, _s2, n));
}
#endif /** _ICMP **/